<template>
    <el-card class="item">
        <template #header>
            <div class="card-header">
                <span>空气湿度</span>
            </div>
        </template>
        <div id="air" ref="chartRef" style="width: 100%;height: 250px;">

        </div>
    </el-card>
</template>


<!-- <script setup>
import { onMounted, watchEffect, ref } from 'vue';
import * as echarts from 'echarts';

let aa = ref(null);


const draw = () => {
    var chartDom = window.document.getElementById('air');
    var myChart = echarts.init(chartDom);
    var option;
    option = {
        xAxis: {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: list.value,
                type: 'line'
            }
        ]
    };
    option && myChart.setOption(option);
}

const props = defineProps({
    air: Array
})

let list = ref(props.air);
watchEffect(() => {
    list.value = props.air;
    if (aa.value) {
        draw();
    } 
})
 
</script> -->

// 修改后的script部分   
<script setup>   
import { ref, watch, onMounted, onBeforeUnmount } from 'vue';   
import * as echarts from 'echarts';
   
const props = defineProps({ air: Array });   
const chartRef = ref(null);   
let chartInstance = null;
   
const initChart = () => {
    if (!chartRef.value || chartInstance) return;
    
    chartInstance = echarts.init(chartRef.value);
    const baseOption = {
        xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] },
        yAxis: { type: 'value' },
        series: [{ type: 'line' }]
    };
    chartInstance.setOption(baseOption);   
};
   
const updateChart = (newData) => {
    if (!chartInstance) return;
    
    chartInstance.setOption({
        series: [{ data: newData }]
    });   
};
   
watch(
    () => props.air,
    (newVal) => {
        if (!chartInstance) initChart();
        updateChart(newVal);
    }   
);
   
onMounted(initChart);      
onBeforeUnmount(() => {
    chartInstance?.dispose();   
});   
</script> 